package com.xdhl.fu.fim.xxljob.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xdhl.fu.common.core.task.IJob;
import com.xdhl.fu.common.core.utils.bean.BeanUtils;
import com.xdhl.fu.fim.repository.MGFlightNumberRepository;
import com.xdhl.fu.fim.service.IFlightInfoService;
import com.xdhl.fu.flight.info.api.domain.FlightInfo;
import com.xdhl.fu.flight.info.api.mongodb.domain.MGFlightNumber;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 航班号同步任务
 *
 * @author 朱力维
 * @time 2022年7月1日
 */
@Component
public class MGFlightNumberSyncJob implements IJob {

    private static final Logger log = LoggerFactory.getLogger(MGFlightNumberSyncJob.class);

    @Resource
    private IFlightInfoService flightInfoService;

    @Resource
    private MGFlightNumberRepository flightNumberRepository;

    @Transactional
    @XxlJob(value = "mgFlightNumberSyncJob", init = "init", destroy = "destroy")
    public ReturnT<String> execute(String param) {
        this.start();
        log.info("航班号已同步完成。");
        return ReturnT.SUCCESS;
    }

    @Override
    public void start() {
        LambdaQueryWrapper<FlightInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(FlightInfo::getDataId, FlightInfo::getFlightNumber);
        List<FlightInfo> list = flightInfoService.list(wrapper);
        flightNumberRepository.deleteAll();
        for (FlightInfo flightInfo : list) {
            MGFlightNumber mgFlightNumber = new MGFlightNumber();
            BeanUtils.copyBeanProp(mgFlightNumber, flightInfo);
            flightNumberRepository.save(mgFlightNumber);
        }
    }

    @Override
    public void init() {

    }

    @Override
    public void destroy() {

    }
}
